package com.laf.manage.controller.user;


import com.laf.common.core.domain.R;
import com.laf.common.exception.ServiceException;
import com.laf.manage.domain.po.AuthInfo;
import com.laf.manage.service.IAuthInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zian
 * @since 2024-11-04
 */
@Slf4j
@Api(tags = "认证信息管理接口")
@RestController
@RequestMapping("/auth-info")
@RequiredArgsConstructor
public class AuthInfoController {


    private final IAuthInfoService authInfoService;

    @GetMapping("/check/auth-status")
    @ApiOperation("检查用户是否已经认证")
    public R<Boolean> checkAuthStatus() {
        Boolean authStatus = authInfoService.checkAuthStatus();
        return R.ok(authStatus);
    }

    /**
     * 上传身份证图片
     */
    @PostMapping("/id-cord")
    @ApiOperation("上传身份证图片")
    public R<String> uploadIDCardImg(
            @ApiParam("带人脸的那一面") @RequestParam(value = "fileFront", required = false) MultipartFile fileFront,
            @ApiParam("待国徽的那一面") @RequestParam(value = "fileBack", required = false) MultipartFile fileBack
    ) {
        if (fileBack == null || fileFront == null) throw new ServiceException("请上传身份证正面和背面");
        log.info("fileFront.name: {}, fileBack.name: {}", fileFront.getName(), fileBack.getName());
        log.info("fileFront.size: {}, fileBack.size: {}", fileFront.getSize(), fileBack.getSize());
        String uuid = authInfoService.uploadIDCardImg(fileBack, fileFront);
        return R.ok(uuid);
    }

    /**
     * 上传人脸认证
     */
    @PostMapping("/face")
    @ApiOperation("上传人脸认证")
    public R<String> uploadFaceImg(@RequestParam(value = "faceFile", required = false) MultipartFile faceFile, String uuid) {
        if (faceFile == null) throw new ServiceException("请上传人脸图片");
        log.info("上传人脸认证：file.size: {} , uuid: {}", faceFile.getSize(), uuid);
        String result = authInfoService.uploadFaceImg(faceFile, uuid);
        return R.ok(result);
    }

    /**
     * 添加认证记录
     */
    @PostMapping
    @ApiOperation("添加认证记录")
    public R addAuth(@RequestParam(value = "fileFront", required = false) MultipartFile fileFront, String uuid) {
        if (fileFront == null) throw new ServiceException("上传身份图片");
        log.info("添加认证记录：file.size: {} , uuid: {}", fileFront.getSize(), uuid);
        authInfoService.addAuth(fileFront, uuid);
        return R.ok();
    }

    /**
     * 删除凭证
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除凭证")
    public R deleteAuthById(@PathVariable Long id) {
        log.info("删除凭证：{}", id);
        authInfoService.deleteAuthById(id);
        return R.ok();
    }

    /**
     * 获取凭证信息
     */
    @GetMapping("{id}")
    @ApiOperation("获取凭证信息")
    public R<AuthInfo> getAuthById(@PathVariable Long id) {
        AuthInfo authInfo = authInfoService.getAuthById(id);
        return R.ok(authInfo);
    }

}
